package com.lzb.musicplayer.activity;

import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;
import com.lzb.musicplayer.BaseAc;
import com.lzb.musicplayer.BasePlayerActivity;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.adapter.MusicAdapter;
import com.lzb.musicplayer.bean.GroupMusicInfo;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.databinding.ActivityRecordBinding;
import com.lzb.musicplayer.dialog.DeleteDialog;
import com.lzb.musicplayer.dialog.MusicEditDialog;
import com.lzb.musicplayer.dialog.PlaylistDialog;
import com.lzb.musicplayer.utils.AppSpUtils;
import com.lzb.musicplayer.utils.MusicScanner;
import com.lzb.musicplayer.utils.RingtoneUtil;
import com.lzb.musicplayer.utils.SaveListUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends BasePlayerActivity<ActivityRecordBinding> implements View.OnClickListener, OnItemClickListener, OnItemChildClickListener {

    private MusicAdapter musicAdapter;
    private List<MusicFile> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecordBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llPlay.setOnClickListener(this);
        mDataBinding.llRandomPlay.setOnClickListener(this);

        mDataBinding.rvMusicList.setLayoutManager(new LinearLayoutManager(mContext));
        musicAdapter = new MusicAdapter();
        mDataBinding.rvMusicList.setAdapter(musicAdapter);
        musicAdapter.addChildClickViewIds(R.id.ivMusicItemEdit);
        musicAdapter.setOnItemClickListener(this);
        musicAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llPlay) {//播放全部
            if (listShow.size() == 0) return;
            mDataBinding.audioPlayerView.startPlay();
        } else if (id == R.id.llPlay) {//随机播放
            if (listShow.size() == 0) return;
            mDataBinding.audioPlayerView.setSj();
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        GroupMusicInfo groupMusicInfo = musicAdapter.getData().get(position);
        List<MusicFile> list = groupMusicInfo.getList();
        Intent intent = new Intent(RecordActivity.this, InfoActivity.class);
        intent.putExtra("list", (Serializable) list);
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivMusicItemEdit) {
            showEditDialog(view, position);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow = AppSpUtils.getRecordList();
        if (listShow != null && listShow.size() > 0) {
            mDataBinding.audioPlayerView.setPlayList(listShow, 0);
            setList(listShow);
        } else {
            mDataBinding.rvMusicList.setVisibility(View.GONE);
            mDataBinding.tvMusicNoData.setVisibility(View.VISIBLE);
        }
    }

    public void setList(List<MusicFile> files) {
        List<GroupMusicInfo> groupedList = MusicScanner.groupByProperty(this, files);
        if (groupedList != null && groupedList.size() > 0) {
            musicAdapter.setList(groupedList);
            mDataBinding.rvMusicList.setVisibility(View.VISIBLE);
            mDataBinding.tvMusicNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvMusicList.setVisibility(View.GONE);
            mDataBinding.tvMusicNoData.setVisibility(View.VISIBLE);
        }

    }

    private void showEditDialog(View view, int position) {
        MusicEditDialog dialog = new MusicEditDialog(mContext);
        BasePopupView basePopupView = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .offsetX(0)//x轴偏移
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(view).hasShadowBg(false) // 去掉半透明背景
                .asCustom(dialog);
        dialog.setListener(new MusicEditDialog.Ilistener() {
            @Override
            public void clickItem(int pos) {
                switch (pos) {
                    case 0://播放
                        setFragmentMusicList(musicAdapter.getItem(position).getList());
                        break;
                    case 1://加入播放队列
                        SaveListUtils.saveDlList(RecordActivity.this, musicAdapter.getItem(position).getList());
                        break;
                    case 2://加入播放列表
                        showPlaylistDialog(musicAdapter.getItem(position).getList());
                        break;
                    case 3://设为铃声
                        RingtoneUtil.setRingtone(mContext, listShow.get(0).getPath(), RingtoneManager.TYPE_RINGTONE);
                        break;
                    case 4://删除
                        showDeleteDialog(position);
                        break;
                }
            }
        });
        basePopupView.show();
    }

    private void showDeleteDialog(int pos) {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setText(getString(R.string.record_delete_tip));
        dialog.setListener(new DeleteDialog.Ilistener() {
            @Override
            public void clickDelete() {
                listShow.remove(pos);
                AppSpUtils.saveRecordList(listShow);
                getData();
            }
        });
        dialog.show();
    }

    private void setFragmentMusicList(List<MusicFile> listFile) {
        if (listFile != null && !listFile.isEmpty()) {
            mDataBinding.audioPlayerView.setPlayList(listFile, 0);
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    private void showPlaylistDialog(List<MusicFile> musicFile) {
        PlaylistDialog playlistDialog = new PlaylistDialog(mContext);
        playlistDialog.setMusicFileList(musicFile);
        playlistDialog.show();
    }

    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.audioPlayerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPlaybackStateChanged(int state) {

    }

    @Override
    public void onProgressUpdated(long position, long duration) {

    }

    @Override
    public void onTrackChanged(MusicFile currentTrack) {

    }
}